package com.library.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.common.Result;
import com.library.dto.BorrowDTO;
import com.library.entity.BorrowRecord;
import com.library.service.BorrowRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 借阅管理控制器
 */
@Tag(name = "借阅管理")
@RestController
@RequestMapping("/borrow")
@RequiredArgsConstructor
public class BorrowController {
    
    private final BorrowRecordService borrowRecordService;
    
    @Operation(summary = "借书")
    @PostMapping
    public Result<Void> borrowBook(@Valid @RequestBody BorrowDTO dto) {
        borrowRecordService.borrowBook(dto.getBookId());
        return Result.success("借阅成功");
    }
    
    @Operation(summary = "还书")
    @PutMapping("/{recordId}/return")
    public Result<Void> returnBook(@PathVariable Long recordId) {
        borrowRecordService.returnBook(recordId);
        return Result.success("归还成功");
    }
    
    @Operation(summary = "续借")
    @PutMapping("/{recordId}/renew")
    public Result<Void> renewBook(@PathVariable Long recordId) {
        borrowRecordService.renewBook(recordId);
        return Result.success("续借成功");
    }
    
    @Operation(summary = "获取我的借阅记录")
    @GetMapping("/my")
    public Result<Page<BorrowRecord>> getMyRecords(@RequestParam(defaultValue = "1") Integer current,
                                                    @RequestParam(defaultValue = "10") Integer size,
                                                    @RequestParam(required = false) String status) {
        Page<BorrowRecord> page = borrowRecordService.getMyRecords(current, size, status);
        return Result.success(page);
    }
    
    @Operation(summary = "分页查询借阅记录（管理员）")
    @SaCheckRole("ADMIN")
    @GetMapping("/records")
    public Result<Page<BorrowRecord>> pageRecords(@RequestParam(defaultValue = "1") Integer current,
                                                   @RequestParam(defaultValue = "10") Integer size,
                                                   @RequestParam(required = false) Long userId,
                                                   @RequestParam(required = false) String status) {
        Page<BorrowRecord> page = borrowRecordService.pageRecords(current, size, userId, status);
        return Result.success(page);
    }
}

